package com.ystarkotlin.base

import android.content.pm.ActivityInfo
import android.opengl.Visibility
import android.os.Bundle
import android.view.View
import android.view.ViewStub
import android.widget.*
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.NetworkUtils
import com.jaeger.library.StatusBarUtil
import com.socks.library.KLog
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity
import com.ystarkotlin.R
import com.ystarkotlin.interfaces.I_InitView
import com.ystarkotlin.interfaces.I_View
import com.ystarkotlin.interfaces.Itoolbar
import com.ystarkotlin.utils.ActivityUtils
import com.ystarkotlin.utils.ImgUtils
import com.ystarkotlin.utils.MyToastUtils
import kotlin.properties.Delegates
import kotlinx.android.synthetic.main.activity_ystar_base.*

/**
 * 基类初始化本身
 */
abstract class YBaseAct : RxAppCompatActivity(), I_View, Itoolbar, I_InitView, ViewStub.OnInflateListener {
    protected val TAG = javaClass.simpleName
    private var isinflate = false //stub是否加载
    protected var isOnErr = false //是否是错误页面
    protected var isshowNetwork = true //是否显示无网络状态 ,默认显示
    protected var mActivity: RxAppCompatActivity by Delegates.notNull()
    protected var loadingimg: ImageView by Delegates.notNull()
    protected var loading_progress: ProgressBar by Delegates.notNull()
    protected var loadingText: TextView by Delegates.notNull()
    protected var bt_reload: Button by Delegates.notNull()
    protected var mrootLayout: LinearLayout? = null
    private var stub_RelativeLayout: RelativeLayout by Delegates.notNull()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ystar_base)
        mActivity = this
        mrootLayout = mrootlayout
        initview()
    }

    /*设置toolbar*/
    fun initview() {
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT//竖屏显示
        setSupportActionBar(this.m_basetoolbar)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        /*默认白色黑字*/
        BarUtils.setStatusBarLightMode(mActivity, true)
        BarUtils.setStatusBarColor(mActivity, ContextCompat.getColor(mActivity, R.color.white))
        base_stub.setOnInflateListener(this)//加载监听
    }

    override fun initdata() {
        if (!NetworkUtils.isConnected()) {
            MyToastUtils.showShort(mActivity, resources.getString(R.string.nonetwork_hint))
            show_Er_Nonet_Layout()
            return
        }
    }

    override fun hide_Layout() {
        stub_RelativeLayout.visibility = View.GONE
    }

    override fun show_LD_Layout() {
        initstub()
        stub_RelativeLayout.visibility = View.VISIBLE
        isOnErr = false
        loading_progress.visibility = View.VISIBLE
        loadingimg.visibility = View.GONE
        loadingText.text = resources.getString(R.string.loadingtext)
        bt_reload.visibility = View.GONE
    }

    override fun show_Er_Layout(msg: String) {
        initstub()
        stub_RelativeLayout.visibility = View.VISIBLE
        isOnErr = true
        loading_progress.visibility = View.GONE
        loadingimg.visibility = View.VISIBLE
        ImgUtils.load(mActivity, R.mipmap.no_data, loadingimg)
        loadingText.text = msg
        bt_reload.visibility = View.VISIBLE
    }

    override fun show_Er_Nonet_Layout() {
        if (isshowNetwork) {
            initstub()
            stub_RelativeLayout.visibility = View.VISIBLE
            isOnErr = true
            loading_progress.visibility = View.GONE
            loadingimg.visibility = View.VISIBLE
            ImgUtils.load(mActivity, R.mipmap.no_data, loadingimg)
            loadingText.text = resources.getString(R.string.nonetwork_hint)
            bt_reload.visibility = View.VISIBLE
        }

    }

    override fun onclicK_Er_Img() {
        initdata()
    }

    override fun setToolBarGone() {
        m_basetoolbar.visibility = View.GONE
    }

    override fun setToolBackGroundColor(color: Int) {
        m_basetoolbar.setBackgroundColor(ContextCompat.getColor(mActivity, color))
    }

    override fun setToolbarTitle(str: String) {
        bast_title.setText(str)
    }

    override fun setEnabledNavigation(isEnable: Boolean) {
        supportActionBar?.setHomeButtonEnabled(isEnable)
        supportActionBar?.setDisplayHomeAsUpEnabled(isEnable)
        m_basetoolbar.setNavigationOnClickListener({ v -> onBackPressed() })
    }


    override fun setEnabledNavigation(isEnable: Boolean, res: Int) {
        setEnabledNavigation(isEnable)
        setNavigatIcon(res)
    }

    override fun setNavigatIcon(res: Int) {
        m_basetoolbar.setNavigationIcon(res)
    }

    override fun showLine() {
        hline.visibility = View.VISIBLE
    }


    protected fun addFragment(fragmentManager: FragmentManager, fragment: Fragment) {
        ActivityUtils.addFragmentToActivity(fragmentManager, fragment, mrootlayout.id)
    }


    /*StubView 加载监听*/
    override fun onInflate(p0: ViewStub?, p1: View?) {
        isinflate = true
    }


    /*设置缓存view*/
    fun initstub() {
        if (isinflate) {
            if (stub_RelativeLayout.getVisibility() == View.GONE)
                stub_RelativeLayout.setVisibility(View.VISIBLE)
        } else {
            try {
                //如果没有被inflate过，使用inflate膨胀
                stub_RelativeLayout = base_stub.inflate() as RelativeLayout
                loadingimg = stub_RelativeLayout.findViewById(R.id.loading_img) as ImageView
                loading_progress = stub_RelativeLayout.findViewById(R.id.loading_progress)!!
                loadingText = stub_RelativeLayout.findViewById(R.id.loading_text)!!
                bt_reload = stub_RelativeLayout.findViewById(R.id.bt_eeror_onclick)!!
                ClickUtils.applyGlobalDebouncing(bt_reload, 2000, View.OnClickListener { onclicK_Er_Img() })
            } catch (e: Exception) {
                //如果使用inflate膨胀报错，就说明已经被膨胀过了，使用setVisibility方法显示
                if (stub_RelativeLayout.getVisibility() == View.GONE)
                    stub_RelativeLayout.setVisibility(View.VISIBLE)
            }

        }
    }

    override fun KLogUtils(message: Any?) {
        KLog.a(TAG, message)
    }
}
